package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class NursingPlanServiceIpml implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    @Override
    public List<NursingPlanVo> getByAll() {
        List<NursingPlan> all = nursingPlanMapper.getByAll();
        List<NursingPlanVo> nursingPlanVos = new ArrayList<>();
        for (NursingPlan nursingPlan : all) {
            NursingPlanVo nursingPlanVo = new NursingPlanVo();
            BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
            nursingPlanVos.add(nursingPlanVo);
        }
        return nursingPlanVos;
    }

    @Override
    public PageResponse<NursingPlanVo> getByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> page = nursingPlanMapper.getByPage(name, status);
        PageResponse<NursingPlanVo> nursingPlanVoPageResponse = PageResponse.of(page, NursingPlanVo.class);
        return nursingPlanVoPageResponse;
    }

    @Override
    @Transactional
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlanMapper.add(nursingPlan);
        List<NursingProjectPlanDto> projectProjectPlans = nursingPlanDto.getProjectPlans();
        if (projectProjectPlans != null && projectProjectPlans.size() != 0) {
            for (NursingProjectPlanDto nursingProjectPlanDto : projectProjectPlans) {
                nursingProjectPlanDto.setPlanId(nursingPlan.getId());
                NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
                BeanUtils.copyProperties(nursingProjectPlanDto, nursingProjectPlan);
                nursingProjectPlanMapper.addProjectPlan(nursingProjectPlan);
            }
        }
    }

    @Override
    public NursingPlanVo getById(Long id) {
        NursingPlanVo nursingPlanVo=nursingPlanMapper.queryById(id);
        return nursingPlanVo;
    }

    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlanMapper.update(nursingPlan);
        nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
        List<NursingProjectPlanDto> projectProjectPlans = nursingPlanDto.getProjectPlans();
        if (projectProjectPlans != null && projectProjectPlans.size() != 0) {
            for (NursingProjectPlanDto nursingProjectPlanDto : projectProjectPlans) {
                nursingProjectPlanDto.setPlanId(nursingPlan.getId());
                NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
                BeanUtils.copyProperties(nursingProjectPlanDto, nursingProjectPlan);
                nursingProjectPlanMapper.addProjectPlan(nursingProjectPlan);
            }
        }
    }

    @Override
    public void delete(Long id) {
        nursingPlanMapper.delete(id);
        nursingProjectPlanMapper.deleteByPlanId(id);
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingPlanMapper.enableOrDisable(id, status);
    }
}
